<?php

namespace Admin\Controller;
use Think\Controller;
use Think\Log;
use Org\Util\Page;  

class WxopenuserController extends BaseController {
	function _initialize() {
		parent::_initialize ();
	}

	public function index() {
		$msg=M("wx_openuser"); 
		$keyword=I('keyword');
		
		$op= isset($_GET['op'])?$_GET['op']:$_POST['op']; 
        $where="1=1"; 
        
		if($keyword)
		{   
		   $where.=" and nickname  like '%".$keyword."%' or country like '%".$keyword."%' or province like '%".$keyword."%' or city like '%".$keyword."%' or remark like '%".$keyword."%'";
		} 
		
		$where= str_replace("1=1 and", "", $where);
		$where= str_replace("1=1", "", $where);
		
	    $count =$msg->where($where)->count(); // 查询满足要求的总记录数

		$Page = new Page ($count, 12 ); // 实例化分页类 传入总记录数和每页显示的记录数
		$Page -> setConfig('header', '条记录');
		$Page -> setConfig('theme', '<li><a>%totalRow% %header%</a></li> <li>%upPage%</li> <li>%downPage%</li> <li>%first%</li>  <li>%prePage%</li>  <li>%linkPage%</li>  <li>%nextPage%</li> <li>%end%</li> ');//(对thinkphp自带分页的格式进行自定义)
		$show = $Page->show (); // 分页显示输出

		$ret=$msg->where($where)->limit( $Page->firstRow . ',' . $Page->listRows )->order("createtime desc")->select();  
		
		$list=array();
		foreach ($ret as $k=>$v){ 
			$v['nickname']=userTextDecode($v['nickname']);  //显示信息对微信昵称进行解码
			$list[]=$v;
		}
		
		$this->assign ( "list", $list);
		$this->assign ( "page", $show ); // 赋值分页输出 
		$this->assign ( "op", $op ); // 赋值分页输出 
		$this->display ();
	} 
	 
	
	//有个传递特价主题编号，一个是没有传主题编号
	public function getwxuser()
	{     
		$wxObj = $this->init();
		$dao=M("wx_openuser"); 
		$op= intval(I("op")); 
		
		$ret=$wxObj->getUserList();
		if(!$ret)exit; 
		if(array_key_exists('errcode',$ret))exit;
		
		$ary=$ret['data']['openid'];
		if(!is_array($ary)){
			$this->error("没有微信用户信息");
		}
		
		foreach($ary as $v)
		{
			if(empty($v))continue; 
			$json=$wxObj->getUserInfo($v); 
			if(!$json ||array_key_exists('errcode',$json))continue; 
			$json['nickname']=userTextEncode($json['nickname']); //添加对微信昵称进行编码
			 
			//对用户信息进行处理 
			$rs=$dao->where(array('openid'=>$v))->order("openid asc")->select();
			if(!$rs)
			{
				$json['createtime']=date("Y-m-d H:i:s"); 
				$rid=$dao->add($json);
			}
			else
			{ 
				$json['openid']=$v;
				$dao->save($json);
			}
		}
		
		$this->redirect('Wxopenuser/index',array('op'=>$op));
	} 
	

	//绑定后台管理员
	public function bind_admin(){
		$data=array();
       $data['openid'] = $_GET['openid']?$_GET['openid']:"";
       if(empty($_GET['openid'])){
       	$this->error('信息传递错误');
       }
       
       $data['bindtype'] = 'admin';
       //先判断该微信用户时候绑定过
       $row = D('wx_binduser')->where($data)->select();
       if($row){
       	$this->error('该微信已经绑定了管理员');
       	$this->redirect("U('Wxopenuser/index')");
       }

       $row2 = D('wx_binduser')->where("user_id = ".$_SESSION['admin_id']." and bindtype = 'admin'")->select();
       if($row2){
       	$this->error('该管理员已经绑定了微信');
       	$this->redirect("U('Wxopenuser/index')");
       }
       $data['user_id']=$_SESSION['admin_id'];
       $data['wx_id'] = getGuid();
       $data['createtime'] = date('Y-m-d H:i:s');
      

       $res = D('wx_binduser')->add($data);
       if(!$res){
         $this->error('绑定失败');
         $this->redirect("U('Wxopenuser/index')");
         exit;
       }else{
       $this->success('绑定成功');
       $this->redirect("U('Wxopenuser/index')");
       exit;
       }

	}
}
?>
